﻿package {
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.events.Event;
	
	import flash.system.Security;
	import flash.events.SecurityErrorEvent;
		
	import flash.errors.IOError;
	import flash.events.IOErrorEvent;
	import flash.system.LoaderContext;
	
	public class Loader2D extends EventDispatcher  {
		public var image:DisplayObject;
		
		private var _loader:Loader;
		public function Loader2D() {
			
		}
		
		public function goLoad(pathToImage:String):void {
			
			//Security.allowDomain("*");
			//Security.allowInsecureDomain("*");
			//Security.loadPolicyFile(Config.serverURL + "/crossdomain.xml");
			
			//var lc:LoaderContext = new LoaderContext();
			//lc.checkPolicyFile = false;
			
			var imageFullPath:String = pathToImage;
			_loader = new Loader();

			_loader.load(new URLRequest(imageFullPath));// , lc);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errrr);
			_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, secur);
		}
		
		private function onImageLoad(e:Event):void{
			image = _loader;
			
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoad);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errrr);
			_loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, secur);
			_loader = null;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}

		private function errrr(event:IOErrorEvent):void
		{
			trace("We have big problem with server or internet connection =( ")
		}
		
		private function secur(event:SecurityErrorEvent):void
		{
			trace("SECURITY_ERROR > низя")
		}
	}
}